/**
 * 鞍山市政府采购网爬虫
 *  
 */
import { Base, Item } from "../base";
class ahcg extends Base  {
    form: any;
    firstPage: string;
    constructor() {
        super();
        this.name = "鞍山市政府采购";
        this.enabled = true;
        this.encode = "utf-8";
        this.debug = false;
        this.timeout = 1000000;
        this.collection = "ztb";
        this.collectionShowName="招投标信息";
        this.keyField = "title";
    }
    getItems(pageIndex: number): Item[] {
        let result: Item[] = [];
        let pageCounts = this.getPageCount();
        let num=pageIndex+1;
        let url = `http://www.asggzyjy.cn/page/index_${num}.jspx?code=biddingNotice_purchase`;
        try {
        let content = this.spider.getSync(url);
        let $ = this.cheerio.load(content);
        let that = this;
        $(".sec_rightlist li").each(function (index, ele) {
            let item: Item = that.buildItem();
            item.title = $(ele).find('a span').eq(0).text().trim()+$(ele).find('a span').eq(1).text().trim();
            (<any>item).url = $(ele).find('a').attr('href');
            let uri = 'http://www.asggzyjy.cn/'+$(ele).find('a').attr('href');
            (<any>item)['采购时间']=$(ele).find('span').text().trim();
            item.source = that.name;
            result=that.getExtraItem(uri, item,result);
        });
        } catch (error) {
            console.log(error);
        }
        return result;
    }
    getPageCount(): number {
        return 102;
    }
    getExtraItem(url: string, item: Item,result:Item[]): Item[] {
        let content:any;
        try {
            content = this.spider.getSync(url);
        } catch (error) {
            console.log(error);
        }
        let $ = this.cheerio.load(content);
        (<any>item).content=$('.text_con').html();
        result.push(item);
        return result;
    }
}
module.exports = new ahcg();